package com.dykj.fireFighting.web.controller;

import com.dykj.fireFighting.api.base.bigdata.Building.entity.Building;
import com.dykj.fireFighting.api.base.bigdata.Building.service.IBuildingService;
import com.dykj.fireFighting.api.base.devHisResult.entity.DevHisResult;
import com.dykj.fireFighting.api.base.devHisResult.service.IDevHisResultService;
import com.dykj.fireFighting.api.base.emergency.entity.EmergencyPlanTwo;
import com.dykj.fireFighting.api.base.emergency.service.IEmergencyPlanTwoService;
import com.dykj.fireFighting.api.base.enterprise.dto.EnterpriseDto;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.system.entity.SysUser;
import com.dykj.fireFighting.api.base.system.service.ISysUserService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import com.dykj.fireFighting.web.base.shiro.jwt.JwtTokenUtil;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(value = "单位信息接口", tags = {"单位信息接口"})
@RestController
@RequestMapping("/socialUnit")
public class UnitController {


    @DubboReference
    private IEnterpriseService enterpriseService;
    @DubboReference
    private ISysUserService iSysUserService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @DubboReference
    private IBuildingService buildingService;
    @DubboReference
    private IEmergencyPlanTwoService emergencyPlanTwoService;
    @DubboReference
    private IDevHisResultService devHisResultService;


    @AutoLog(value = "查询监控中心所有社会单位")
    @ApiOperation(value = "查询监控中心所有社会单位", notes = "查询监控中心所有社会单位")
    @GetMapping("/list")
    public CommonResult<List<Enterprise>> list() {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        String unitId = String.valueOf(loginUserVo.getEnterpriseId());
        List<Enterprise> list = enterpriseService.queryUnitSelect(unitId);
        return CommonResult.success(list);
    }

    @AutoLog(value = "查询监控中心所有社会单位")
    @ApiOperation(value = "查询监控中心所有社会单位", notes = "查询监控中心所有社会单位")
    @GetMapping("/pageList")
    public CommonResult<PageVo<Enterprise>> pagelist(EnterpriseDto enterpriseDto) {
        LoginUserVo loginUserVo = ShiroUtil.getUser();
        String unitId = String.valueOf(loginUserVo.getEnterpriseId());
        enterpriseDto.setEnterpriseCode(unitId);
        PageVo<Enterprise> pageVo = enterpriseService.queryUnitSelectPage(enterpriseDto);
        return CommonResult.success(pageVo);
    }


    @AutoLog(value = "切换到社会单位")
    @ApiOperation(value = "切换到社会单位", notes = "切换到社会单位")
    @GetMapping("/changeView")
    public CommonResult<String> changeView(@RequestParam String unitId) {
        SysUser sysUser = iSysUserService.getSocialShowUser(unitId);
        if (sysUser == null) {
            return CommonResult.failed("该社会单位未开通演示账号");
        }
        //生成随意key,生效时间60秒
        Map<String, Object> map = new HashMap<>();
        map.put("userName", sysUser.getLoginName());
        map.put("password", sysUser.getPassword());
        String key = jwtTokenUtil.generateToken(map);
        redisService.set(key, new Gson().toJson(sysUser, SysUser.class), Long.parseLong("60"));
        return CommonResult.success(key);

    }

    @AutoLog(value = "查询楼栋信息")
    @ApiOperation(value = "查询楼栋信息", notes = "查询楼栋信息")
    @GetMapping("/getBuildIngInfo")
    public CommonResult<Map<String, Object>> getBuildIngInfo(@RequestParam String unitId, String buildingId) {
        Map<String, Object> map = new HashMap<>();
        Enterprise enterprise = enterpriseService.getById(unitId);
        DevHisResult devHisResult=devHisResultService.getUnitNum(unitId,null,null);
        map.put("devHisResult", devHisResult);
        map.put("enterprise", enterprise);
        if (StringUtils.isNotBlank(buildingId)) {
            Building building = buildingService.getById(buildingId);
            map.put("building", building);
            EmergencyPlanTwo emergencyPlanTwo = emergencyPlanTwoService.SelectInfoByBuildingId(buildingId);
            if (null != emergencyPlanTwo) {
                map.put("buildingHaveEmergencyPlan", "0");
            } else {
                map.put("buildingHaveEmergencyPlan", "1");
            }
        }
        return CommonResult.success(map);
    }

}
